﻿using System;
using System.IO;

namespace FileDuplicateRemover.Core
{
    public class EntityRepository
    {
        public DirectoryEntity GetDirectoryEntity(string rootDirectoryFullName, Func<int, bool> notify)
        {
            DirectoryEntity de = null;

            using (EntityTreeBuilder eb = new EntityTreeBuilder(rootDirectoryFullName))
            {
                de = eb.Create(notify);
            }

            return de;
        }

        public DuplicateEntityDescriptionList GetDuplicateEntityDescriptionList(DirectoryEntity de, Func<int, bool> notify)
        {
            DuplicateEntityDescriptionList duplicateEntityList = new DuplicateEntityDescriptionList();
            EntityTreeWalker ec = new EntityTreeWalker();

            int i = 0;
            ec.Walk(de, (e) =>
            {
                duplicateEntityList.Add(e);

                bool res = notify(i);
                i++;

                return res;
            });

            return duplicateEntityList;
        }

        public void DeleteDirectoryEntity(DirectoryEntity de)
        {
            Directory.Delete(de.FullName, true);
            //System.Threading.Thread.Sleep(500);
        }

        public void DeleteFileEntity(FileEntity fe)
        {
            File.Delete(fe.FullName);
            //System.Threading.Thread.Sleep(500);
        }

        public int GetTotalEntitiesCount(string rootDirectoryFullName)
        {
            string[] fileSystemEntries = Directory.GetFileSystemEntries(rootDirectoryFullName, "*", SearchOption.AllDirectories);

            return fileSystemEntries.Length;
        }

        public void CalcHash(DirectoryEntity de, Func<int, bool> notify)
        {
            EntityTreeWalker ec = new EntityTreeWalker();

            int i = 0;
            ec.Walk(de, (e) =>
            {
                if (e is FileEntity)
                {
                    byte[] hash = ((FileEntity)e).Hash; // Вычисление хэша
                }

                bool res = notify(i);
                i++;

                return res;
            });
        }
    }
}
